package info.thereisonlywe.core.search;


public class SearchEngine {
	
	/**
     * Calling class handles scope itself
     * @param text
     * @param phrase
     * @param modifier
     * @return matches or not
     */
    public static boolean search(String text, String phrase, SearchModifier modifier)
    {
    	if (modifier == SearchModifier.CONTAINS)
    		return contains(text, phrase);
    	else if (modifier == SearchModifier.EXACT)
            return exact(text, phrase);
    	else if (modifier == SearchModifier.STARTS_WITH)
            return startsWith(text, phrase);
    	else if (modifier == SearchModifier.ENDS_WITH)
            return endsWith(text, phrase);
    	else
    		return false;
    }
    
    public static boolean exact(String text, String phrase)
    {
        
        if (text.equals(phrase))
            return true;
        else if (text.contains(" " + phrase + " "))
            return true;
        else if (text.contains(" " + phrase) && text.endsWith(phrase))
            return true;
        else if (text.contains(phrase + " ") && text.startsWith(phrase))
            return true;
        else
            return false;
    }
    
    public static boolean startsWith(String text, String phrase){
        
        if (text.contains(" "+phrase)){
            return true;
        }
        else if (text.length() >= phrase.length()){
            if (new String(text.substring(0, phrase.length())).
                    equals(phrase)){
               return true;
            }
            else
            	return false;
        }
        else
        	return false;
    }
    
    public static boolean endsWith(String text, String phrase){
    	
    	if (text.contains(phrase+" ")){
            return true;
        }
        else if (text.length() > phrase.length()){
            if (new String(text.substring((text.length() - phrase.
                    length()), text.length())).equals(phrase)){
                 return true;
            }
            else if (new String(text.substring((text.length() -
                    phrase.length()-1), text.length()-1)).
                            equals(phrase)) {
                 return true;
            }
        }
        else if (text.length() == phrase.length() &&
                text.contains(phrase)){
        	return true;
        }
        return false;
    }
    
    public static boolean contains(String text, String phrase){
    	
    	if (text.contains(phrase))
        {
            return true;
        }
        return false;
    }

}
